package com.albert.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class ShadowLayout extends FrameLayout {
    private Paint shadowPaint;
    private int shadowColor = Color.parseColor("#33000000"); // 阴影颜色（淡黑）
    private float shadowRadius = 20f;  // 阴影模糊半径
    private float dx = 0;              // X 方向偏移
    private float dy = 0;              // Y 方向偏移
    private float cornerRadius = 0f;   // 圆角半径

    public ShadowLayout(Context context, float dx, float dy) {
        super(context);
        this.dx = dx;
        this.dy = dy;
        init();
    }

    public ShadowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShadowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        setLayerType(LAYER_TYPE_SOFTWARE, null); // 关闭硬件加速

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 内容区域略微缩进，避免左/上/下方向的阴影
        shadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        shadowPaint.setStyle(Paint.Style.FILL);
        shadowPaint.setColor(Color.TRANSPARENT);
        shadowPaint.setShadowLayer(shadowRadius, dx, dy, shadowColor);
        RectF rect = new RectF(
                shadowRadius,
                shadowRadius,
                getWidth(),
                getHeight()
        );
        canvas.drawRoundRect(rect, cornerRadius, cornerRadius, shadowPaint);
    }
}
